package noobteam.deartrainer.controller;

import noobteam.deartrainer.mapper.UserInfoMapper;
import noobteam.deartrainer.service.CourseInfoService;
import noobteam.deartrainer.service.CourseSelectionsService;
import noobteam.deartrainer.service.UserInfoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 徐紫印
 */
@RestController
public class CourseSelectionsController {

    @Resource
    private CourseSelectionsService courseSelectionsService;
    @Resource
    private CourseInfoService courseInfoService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private BudgetInfoController budgetInfoController;

    /**
     * 选课
     * @param map 包括学员号和课程号，均为Integer类型
     * @return 返回操作是否成功
     */
    @RequestMapping("/selectCourse")
    public String selectCourse(@RequestParam Map<String,String> map) {
        double price = courseInfoService.getCoursePrice(Integer.parseInt(map.get("courseId")));
        double account = userInfoService.getUserAccount(Integer.parseInt(map.get("userId")));
        if (price > account) {
            return "余额不足!";
        } else {
            String userId = map.get("userId");
            Map<String, Object> user = new HashMap<>();
            user.put("userId", userId);
            user.put("price", price);
            try {
                boolean isSucceed = courseSelectionsService.selectCourse(map);
                if (isSucceed) {
                    userInfoService.buyCourse(user);
                    courseInfoService.addStudent(Integer.parseInt(map.get("courseId")));

                    Map<String, Object> budget = new HashMap<>();
                    budget.put("userId", Integer.parseInt(userId));
                    budget.put("itemType", "消费");
                    budget.put("itemMoney", price);
                    budget.put("currentUseBalance", userInfoService.getUserAccount(Integer.parseInt(userId)));
                    Calendar calendar = Calendar.getInstance();
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String dateTime = formatter.format(calendar.getTime());
                    budget.put("itemDateTime", dateTime);
                    budgetInfoController.createBudget(budget);

                    return "购买成功!";
                } else {
                    return "你已经购买过课程了！";
                }
            } catch (Exception e) {
                return "你已经购买过课程了！";
            }
        }
    }

    @RequestMapping("/dropCourse")
    public String dropCourse(@RequestParam Map<String, String> map) {

        try {
            boolean isSucceed = courseSelectionsService.dropCourse(map);
            if (isSucceed) {
                Map<String, String> user = new HashMap<>();
                user.put("userId", map.get("userId"));
                user.put("money", courseInfoService.getCoursePrice(Integer.parseInt(map.get("courseId"))) + "");
                userInfoService.addMoney(user);
                courseInfoService.deleteStudent(Integer.parseInt(map.get("courseId")));

                Map<String, Object> budget = new HashMap<>();
                Calendar calendar = Calendar.getInstance();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateTime = formatter.format(calendar.getTime());
                budget.put("userId", Integer.parseInt(map.get("userId")));
                budget.put("itemType", "退款");
                budget.put("itemMoney", courseInfoService.getCoursePrice(Integer.parseInt(map.get("courseId"))));
                budget.put("currentUseBalance", userInfoService.getUserAccount(Integer.parseInt(map.get("userId"))));
                budget.put("itemDateTime", dateTime);
                budgetInfoController.createBudget(budget);

                return "退课成功";
            }
        } catch (Exception e){
            return "您没有购买这门课！";
        }
        return "您没有购买这门课";
    }

    /**
     * 删除某个学员所有的选课记录
     * @param userId 学员id
     * @return 操作是否完成
     */
    @RequestMapping("/deleteSelectionOfStudent")
    public boolean deleteSelectionOfStudent(@RequestParam Integer userId) {
        return courseSelectionsService.deleteSelectionOfStudent(userId);
    }

    @RequestMapping("/deleteSelectionOfCourse")
    public boolean deleteSelectionOfCourse(@RequestParam Integer courseId) {
        return courseSelectionsService.deleteSelectionOfCourse(courseId);
    }

    @RequestMapping("/getCourseSelectionNum")
    public int getCourseSelectionNum(@RequestParam Integer courseId) {
        return courseSelectionsService.getCourseSelectionNum(courseId);
    }

    @RequestMapping("/getStudentSelectionNum")
    public int getStudentSelectionNum(@RequestParam Integer userId) {
        return courseSelectionsService.getStudentSelectionNum(userId);
    }

    @RequestMapping("/getStudentListByCourseId")
    public List<Map<String, Object>> getStudentListByCourseId(@RequestParam Integer courseId) {
        return courseSelectionsService.getStudentListByCourseId(courseId);
    }

    @RequestMapping("/getCourseSelectedList")
    public List<Map<String, Object>> getCourseSelectedList(@RequestParam Integer userId) {
        return courseSelectionsService.getCourseSelectedList(userId);
    }

    @RequestMapping("/getStudentListByCourseTypeId")
    public List<Map<String, Object>> getStudentListByCourseTypeId(@RequestParam int courseTypeId){
        return courseSelectionsService.getStudentListByCourseTypeId(courseTypeId);
    }
}
